// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// T [author] rhyme_lph
// p [email]  rhymelph@gmail.com
// Q [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/payment_detail_entity.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/button/normal_float_svg_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_text_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_text_white_button.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/select/payment_item_checkbox.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/bill/see_payment_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "seePaymentPage")
class SeePaymentPage extends StatefulWidget {
  final int billId;
  final String billNumber;
  final int invoiceId;
  final String invoiceNumber;
  final int expenseId;
  final int creditId;
  final int debitId;
  final bool isClosed;

  const SeePaymentPage(
      {Key key,
      this.billId,
      this.expenseId,
      this.isClosed,
      this.billNumber,
      this.invoiceId,
      this.invoiceNumber,
      this.creditId,
      this.debitId})
      : super(key: key);

  @override
  _SeePaymentPageState createState() => _SeePaymentPageState();
}

class _SeePaymentPageState extends State<SeePaymentPage>
    with PagePresenter<SeePaymentPage, SeePaymentProvider> {
  @override
  void initState() {
    provider.billId = widget.billId;
    provider.billNumber = widget.billNumber;
    provider.invoiceId = widget.invoiceId;
    provider.invoiceNumber = widget.invoiceNumber;
    provider.expenseId = widget.expenseId;
    provider.creditId = widget.creditId;
    provider.debitId = widget.debitId;
    provider.isClosed = widget.isClosed;
    if (widget.isClosed == true) {
      provider.menuList = ["Delete"];
    }
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
          titleWidget: listBuilder<bool>(
            selector: (_, __) => __.isDeleteModel,
            builder: (BuildContext context, bool value, Widget child) =>
                Text(value ? S.current.delete_payment : S.current.see_payment),
          ),
          actions: [
            listBuilder<bool>(
              selector: (_, __) => __.isDeleteModel,
              builder: (BuildContext context, bool value, Widget child) =>
                  value == true ? Container() : _buildPopButton(),
            ),
          ]);

  Widget _buildPopButton() => NormalMorePopupButton(
        onSelect: provider.onSelectMenu,
        menuList: provider.menuList,
      );

  @override
  Widget get buildEmptyWidget => Stack(
        children: [
          super.buildEmptyWidget,
          Positioned(
              bottom: getDp(32) + MediaQuery.of(context).padding.bottom,
              left: 0.0,
              right: 0.0,
              child: _buildBottomBar()),
        ],
      );

  @override
  Widget get buildWidget => Stack(
        children: [
          listBuilder<int>(
            selector: (_, __) => List.from(__.data).length,
            builder: (BuildContext context, int value, Widget child) =>
                SmartRefresher(
              controller: provider.refreshController,
              onRefresh: provider.onRefresh,
              onLoading: provider.onLoadMore,
              enablePullUp: false,
              child: ListView.builder(
                itemBuilder: _buildItems,
                itemCount: value,
              ),
            ),
          ),
          Positioned(
              bottom: getDp(32) + MediaQuery.of(context).padding.bottom,
              left: 0.0,
              right: 0.0,
              child: _buildBottomBar()),
        ],
      );

  Widget _buildBottomBar() => listBuilder<bool>(
      selector: (_, __) => __.isDeleteModel,
      builder: (BuildContext context, bool value, Widget child) => IndexedStack(
            alignment: Alignment.center,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  NormalFloatTextWhiteButton(
                    title: S.current.cancel,
                    onTap: provider.onCancel,
                  ),
                  SizedBox(
                    width: getDp(15),
                  ),
                  NormalFloatTextButton(
                    title: S.current.delete,
                    onTap: provider.onDelete,
                  ),
                ],
              ),
              listBuilder<bool>(
                  selector: (_, __) => __.isClosed,
                  builder: (BuildContext context, bool value, Widget child) =>
                      value == false
                          ? NormalFloatSvgButton(
                              onTap: provider.onAdd,
                              asset: A.assetsImagesDashboardAdd,
                            )
                          : Container()),
            ],
            index: value ? 0 : 1,
          ));

  Widget _buildItems(BuildContext context, int index) {
    return listBuilder<Tuple3<PaymentDetailEntity, bool, bool>>(
      selector: (_, __) => Tuple3(__.data[index],
          __.currentSelect.contains(__.data[index]), __.isDeleteModel),
      builder: (BuildContext context,
              Tuple3<PaymentDetailEntity, bool, bool> value, Widget child) =>
          PressedOpacityButton(
        onPressed: () {
          if (value.item3 == true) {
            provider.onAddPayment(value.item1);
          } else {
            provider.onNavDetail(value.item1);
          }
        },
        child: PaymentItemCheckbox(
          type: value.item1.flowType,
          title: value.item1.contact.name,
          date: DateTimeUtils.formatDateTimeString(value.item1.issuedAt),
          unit: value.item1.currencyCode,
          amount: double.parse(value.item1.pivot.amount),
          isSelect: value.item2,
          isSelectMode: value.item3,
        ),
      ),
    );
  }
}
